package model.user;

/**
 * This class is used to keep track of user statistics such as,
 * ranking, hands played and won, and experience point
 */

import java.io.Serializable;
import model.user.User.Ranking;
import java.sql.*;

public class UserStatistics implements Serializable {
	private Ranking rank;
	private int handsPlayed;
	private int handsWon;
	private int experiencePoints;
	
	/**
	 * Default constructor, creates userStatistics object setting the statistics to initial values
	 * The rank is set to the lowest value which is SCRUB
	 */
	public UserStatistics() {
		rank = Ranking.SCRUB;
		handsPlayed = 0;
		handsWon = 0;
		experiencePoints = 0;
	}
	
	/**
	 * Constructor which takes in specific statistics to set for the user
	 */
	public UserStatistics(Ranking rank, int handsPlayed, int handsWon, int experiencePoints) {
		this.rank = rank;
		this.handsPlayed = handsPlayed;
		this.handsWon = handsWon;
		this.experiencePoints = experiencePoints;
	}
	
	//Getters and setters
	
	public Ranking getRank() {
		return rank;
	}
	public void setRank(Ranking rank) {
		this.rank = rank;
	}

	public int getHandsPlayed() {
		return handsPlayed;
	}
	
	public void setHandsPlayed(int handsPlayed) {
		this.handsPlayed = handsPlayed;
	}
	
	public int getHandsWon() {
		return handsWon;
	}
	
	public void setHandsWon(int handsWon) {
		this.handsWon = handsWon;
	}
	
	public int getExperiencePoints() {
		return experiencePoints;
	}
	
	public void setExperiencePoints(int experiencePoints) {
		this.experiencePoints = experiencePoints;
	}

	/**
	 * Player won a hand, update the statistics to reflect the victory and update the database
	 * @param username the name of the user who won the hand
	 * @param winnings the amount of credit the user has won (used to calculate the experience points gained)
	 */
	public void wonHand(String username, int winnings) {
		handsWon++;
		handsPlayed++;
		
		//if the user has won less than 10 chips, increment the exp by only 1
		if(winnings < 10){
			experiencePoints += experiencePoints + 1;
		}
		//if the winnings were 10 or more chips, increment the exp by 1 per 10 chips won
		else{
			experiencePoints += winnings/10;
		}
		
		//if the user has surpassed the number of point needed to get to the next rank, increase the users rank
		if (experiencePoints >= rank.getNextLevelPoints()) {
			rank = Ranking.valueOf(rank.getNextRank());
		}
		
		//determine user ranking integer
		int userRank = 1;
		if(rank.getRank().equals("STUD"))
			userRank = 0;
		else if(rank.getRank().equals("SCRUB"))
			userRank = 1;
		else if(rank.getRank().equals("NEWBIE"))
			userRank = 2;
		else if(rank.getRank().equals("NOVICE"))
			userRank = 3;
		else if(rank.getRank().equals("AMATUER"))
			userRank = 4;
		else if(rank.getRank().equals("SEASONED"))
			userRank = 5;
		else if(rank.getRank().equals("SKILLED"))
			userRank = 6;
		else if(rank.getRank().equals("SEMIPRO"))
			userRank = 7;
		else if(rank.getRank().equals("ADEPT"))
			userRank = 8;
		else if(rank.getRank().equals("PRO"))
			userRank = 9;
		else if(rank.getRank().equals("MASTER"))
			userRank = 10;
		
		//update the database
		try {
			Statement stmt;
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://instance10905.db.xeround.com:7414/fcsgroup2"; //Location of Database
			
			//Connect to the Database
			Connection con = DriverManager.getConnection(url,"fcsgroup2", "qwerty123");

			System.out.println("URL: " + url);
			System.out.println("Connection: " + con);
				
			//Get a Statement Object
			stmt = con.createStatement();
				
			String update = "UPDATE users SET HandsPlayed="+handsPlayed+", HandsWon="+handsWon+", "
					+"ExperiencePoints="+experiencePoints+", Rank="+userRank+" WHERE Username='"+username+"'";
			stmt.executeUpdate(update);
				
			stmt.close();
			con.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Player lost a hand, update the statistics to reflect the loss and update the database
	 * @param username the name of the user who lost the hand
	 */
	public void lostHand(String username) {
		handsPlayed++;
		try {
			Statement stmt;
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://instance10905.db.xeround.com:7414/fcsgroup2"; //Location of Database
			
			//Connect to the Database
			Connection con = DriverManager.getConnection(url,"fcsgroup2", "qwerty123");

			System.out.println("URL: " + url);
			System.out.println("Connection: " + con);
			
			//Get a Statement Object
			stmt = con.createStatement();
			
			String update = "UPDATE users SET HandsPlayed="+handsPlayed+" WHERE Username='"+username+"'";
			stmt.executeUpdate(update);
			
			stmt.close();
			con.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}
